package edu.iupui.csci.mortgageplanner;

import java.util.List;

import edu.iupui.csci.mortgageplanner.util.Mortgage;
import edu.iupui.csci.mortgageplanner.util.MortgagePeriodDetails;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class AmortizationMonthListActivity extends ListActivity {
	
	Long yearId;

	@Override
	protected void onResume() {
		super.onResume();

		List<MortgagePeriodDetails> mortgageDetails = Mortgage.getMortgage().getMortgageDetails();
		String[] values = new String[]{"Month        Interest        Principal","JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
		for(int i = 1; i<= 12; i++) {
			int index = new Long((yearId -1) * 12 + (i -1)).intValue();
			if(index >= mortgageDetails.size()) {
				index = mortgageDetails.size() -1;
				values[i] = "";
				continue;
			}
			values[i] += ", " + (2013 + yearId -1 ) + "  " + 
										String.format("%1$,10.2f", mortgageDetails.get(index).getTotalInterest()) + "  " + 
										String.format("%1$,10.2f", mortgageDetails.get(index).getTotalPrincipal());;
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		        android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//setContentView(R.layout.activity_amortization_year_list);
		// Show the Up button in the action bar.
		setupActionBar();
		
		
		TextView headerTextView = (TextView) findViewById(R.id.year_label_id);
		ListView monthListView = getListView();
		
		yearId = getIntent().getLongExtra("yearId",0);

		

	    monthListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		    	Long yearId = getIntent().getLongExtra("yearId", 0);
		    	Long index = (yearId -1) * 12  + id;
		    	if(index <= Mortgage.getMortgage().getMortgageDetails().size()) {
		    		String[] months = new String[]{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
			    	Intent intent = new Intent(getApplicationContext(),MonthDetailActivity.class);
					intent.putExtra("year", getIntent().getStringExtra("year"));
					intent.putExtra("yearId", getIntent().getLongExtra("yearId", 0));
					intent.putExtra("monthId", id);
					intent.putExtra("title",months[new Long(id -1).intValue()] + ", " + (2013 + getIntent().getLongExtra("yearId", 0) -1));
					startActivity(intent);
		    	
		    	}
		    }
		});
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		try{
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}catch(Throwable e) {
			
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.amortization_month_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			//NavUtils.navigateUpFromSameTask(this);
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
